local xiese = fk.CreateSkill {
  name = "3s__xiese",
}

local DIY = require "packages/diy_utility/diy_utility"

Fk:loadTranslationTable{
  ["3s__xiese"] = "协色",
  [":3s__xiese"] = "一名角色的回合结束时，若其本回合失去牌中较多颜色与本回合弃牌堆中颜色较多的牌相同，你可令其执行一个所有角色仅能使用和打出此颜色牌的额外出牌阶段。",
  ["#3s__xiese-trigger"] = "协色：你可以令%src执行额外出牌阶段，所有角色仅能使用和打出 %arg 牌。",
  ["@3s__xiese-turn"] = "协色",
  
  ["$3s__xiese1"] = "",
  ["$3s__xiese2"] = "",
}

xiese:addAcquireEffect(function (self, player, is_start)
  player.room:addSkill("#CenterArea")
end)

-- 使用事件记录器获取本回合失去牌和弃牌堆的牌
xiese:addEffect(fk.EventPhaseEnd, {
  can_trigger = function(self, event, target, player, data)
    local room = player.room
    if target:isAlive() and player:hasSkill(xiese.name) and #DIY.getCenterCards(room) > 0 and target.phase == Player.Finish then
      local lostCards = {}
      room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function (e)
        for _, move in ipairs(e.data) do
          if move.from == target then
            for _, info in ipairs(move.moveInfo) do
              if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
                table.insertIfNeed(lostCards, info.cardId)
              end
            end
          end
        end
      end, Player.HistoryTurn)
      -- 获取本回合弃牌堆的牌
      local discardCards = DIY.getCenterCards(room)
      -- 计算失去牌的较多颜色
      local lostRed = #table.filter(lostCards, function (id) return Fk:getCardById(id).color == Card.Red end)
      local lostBlack = #table.filter(lostCards, function (id) return Fk:getCardById(id).color == Card.Black end)
      if lostRed == lostBlack then return end
      local dominantLostColor = (lostRed > lostBlack) and "red" or "black"
      -- 计算弃牌堆的较多颜色
      local discardRed = #table.filter(discardCards, function (id) return Fk:getCardById(id).color == Card.Red end)
      local discardBlack = #table.filter(discardCards, function (id) return Fk:getCardById(id).color == Card.Black end)
      if discardRed == discardBlack then return end
      local dominantDiscardColor = (discardRed > discardBlack) and "red" or "black"
      event:setCostData(self, dominantLostColor)
      return dominantLostColor == dominantDiscardColor
    end
  end,
  on_cost = function(self, event, target, player, data)
    local color = event:getCostData(self)
    return player.room:askToSkillInvoke(player, { skill_name = xiese.name, prompt = "#3s__xiese-trigger:" .. target.id .. "::" .. color })
  end,
  on_use = function(self, event, target, player, data)
    local color = event:getCostData(self)
    player.room:addTableMarkIfNeed(target, "@3s__xiese-turn", color)
    -- 执行额外出牌阶段
    target:gainAnExtraPhase(Player.Play, xiese.name, true, {skill_name = xiese.name})
    --player.room:removeTableMark(target, "@3s__xiese-turn", color)
  end,
})

xiese:addEffect(fk.EventPhaseEnd, {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(xiese.name, true, true) and data.extra_data and data.extra_data.skill_name == xiese.name
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(target, "@3s__xiese-turn", 0)
  end,
})



-- 颜色限制效果
xiese:addEffect("prohibit", {
  prohibit_use = function (self, player, card)
    if Fk:currentRoom().current and Fk:currentRoom().current:getMark("@3s__xiese-turn") ~= 0 then
      if Fk:currentRoom().current.phase == Player.Play then
        return not table.contains(player:getTableMark("@3s__xiese-turn"), card:getColorString())
      end
    end
  end,
  prohibit_response = function(self, player, card)
    if Fk:currentRoom().current and Fk:currentRoom().current:getMark("@3s__xiese-turn") ~= 0 then
      if Fk:currentRoom().current.phase == Player.Play then
        return not table.contains(player:getTableMark("@3s__xiese-turn"), card:getColorString())
      end
    end
  end,
})

return xiese